﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Entity.Package;
using Entity.Table;
using Tools;

namespace BLL.Business
{
    public class StrikeBalance
    {
        /// <summary>
        ///  冲帐业务
        /// </summary>
        public static void ProcessStrikeBalance(PackRequestStrikeBalance detail,CSocket.ClientThread client)
        {
            try
            {
                MStrikeBalance balance = new MStrikeBalance("NXS",
                                                          detail.packHead.PayWay.Trim(),
                                                          detail.BankWaterNumber.Trim(),
                                                          detail.GasUserID.Trim());
                int isWriteCard = 0;
                if (Db.StrikeBalanceManager.IsExistTradeFlow(balance.BankWaterID, balance.GasUserID,balance.PaymentType,out isWriteCard))  //存在交费流水
                {
                    if (isWriteCard == 1)
                    {
                        PackReturnStrikeBalance pkStrikeReturn = new PackReturnStrikeBalance();
                        pkStrikeReturn.TradeCode = ((int)TradeCode.缴费冲帐).ToString();
                        pkStrikeReturn.ResponseCode = ((int)ResponseCode.已写卡不允许冲正).ToString().PadLeft(3, '0');
                        pkStrikeReturn.GasUserID = detail.GasUserID.Trim();
                        pkStrikeReturn.PayMoney = "";
                        pkStrikeReturn.PackHead = new PackHead(detail.packHead.PayWay, detail.packHead.BankWaterNumber, (Encoding.Default.GetBytes(pkStrikeReturn.ToString()).Length - 1).ToString());

                        byte[] sendPackage = CSocket.PackageHandle.CombinationPackage(pkStrikeReturn);
                        if (client.Send(sendPackage))  //返回包发送成功，断开链接
                            client.CloseSocket();
                        return;
                    }
                    string payMoney = "0";
                    if (Db.StrikeBalanceManager.PerformStrikeBalance(balance,out payMoney))
                    {
                        LogHelper.WriteInfoLog("冲帐成功，冲帐包内容为：" + detail.ToString());
                        PackReturnStrikeBalance pkStrikeReturn = new PackReturnStrikeBalance();
                        pkStrikeReturn.TradeCode = ((int)TradeCode.缴费冲帐).ToString();
                        pkStrikeReturn.ResponseCode = ((int)ResponseCode.交易成功).ToString().PadLeft(3, '0');
                        pkStrikeReturn.GasUserID = detail.GasUserID.Trim();
                        pkStrikeReturn.BankWaterNumber = detail.BankWaterNumber;
                        pkStrikeReturn.PayMoney = payMoney;
                        pkStrikeReturn.PackHead = new PackHead(detail.packHead.PayWay, detail.packHead.BankWaterNumber, (Encoding.Default.GetBytes(pkStrikeReturn.ToString()).Length).ToString());
                        byte[] sendPackage = CSocket.PackageHandle.CombinationPackage(pkStrikeReturn);
                        if (client.Send(sendPackage))  //发送成功，关闭链接
                            client.CloseSocket();
                    }
                    else  //冲帐失败
                    {
                        PackReturnStrikeBalance pkStrikeReturn = new PackReturnStrikeBalance();
                        pkStrikeReturn.TradeCode = ((int)TradeCode.缴费冲帐).ToString();
                        pkStrikeReturn.ResponseCode = ((int)ResponseCode.交易失败).ToString().PadLeft(3, '0');
                        pkStrikeReturn.GasUserID = detail.GasUserID.Trim();
                        pkStrikeReturn.PayMoney = "";
                        pkStrikeReturn.PackHead = new PackHead(detail.packHead.PayWay, detail.packHead.BankWaterNumber, (Encoding.Default.GetBytes(pkStrikeReturn.ToString()).Length - 1).ToString());

                        byte[] sendPackage = CSocket.PackageHandle.CombinationPackage(pkStrikeReturn);
                        if (client.Send(sendPackage))  //返回包发送成功，断开链接
                            client.CloseSocket();
                        LogHelper.WriteInfoLog("冲帐失败");
                    }
                }
                else  //不存在待冲账的交费流水
                {
                    PackReturnStrikeBalance pkStrikeReturn = new PackReturnStrikeBalance();
                    pkStrikeReturn.TradeCode = ((int)TradeCode.缴费冲帐).ToString();
                    pkStrikeReturn.ResponseCode = ((int)ResponseCode.交易不存在).ToString().PadLeft(3, '0');
                    pkStrikeReturn.GasUserID = detail.GasUserID.Trim();
                    pkStrikeReturn.PayMoney = "";
                    pkStrikeReturn.PackHead = new PackHead(detail.packHead.PayWay, detail.packHead.BankWaterNumber, (Encoding.Default.GetBytes(pkStrikeReturn.ToString()).Length - 1).ToString());

                    byte[] sendPackage = CSocket.PackageHandle.CombinationPackage(pkStrikeReturn);
                    if (client.Send(sendPackage))
                        client.CloseSocket();
                    LogHelper.WriteInfoLog("不存在待冲账的交费流水");
                }
            }
            catch (Exception ex)
            {
                LogHelper.WriteErrorLog("ProcessStrikeBalance方法-冲帐业务出现异常，异常信息：" + ex.Message,ex);
                PackFailReturn pkFailReturn = new PackFailReturn();
                pkFailReturn.TradeCode = ((int)TradeCode.缴费冲帐).ToString();
                pkFailReturn.ResponseCode = ((int)ResponseCode.其他错误).ToString().PadLeft(3,'0');
                pkFailReturn.PackHead = new PackHead(detail.packHead.PayWay, detail.packHead.BankWaterNumber, (Encoding.Default.GetBytes(pkFailReturn.ToString()).Length).ToString());
                CSocket.PackageHandle.SendFailurPackage(pkFailReturn, client);
            }
        }
    }
}
